package com.zunipe.ftpe.util;

import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {
    public static String getCurrentTimePoint() {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        return sdf.format(System.currentTimeMillis());
    }

    public static String formatTime(long milliseconds) {
        long totalSeconds = milliseconds / 1000;
        long hours = totalSeconds / 3600;
        long remainingSeconds = totalSeconds % 3600;
        long minutes = remainingSeconds / 60;
        long seconds = remainingSeconds % 60;
        if (hours > 0) {
            return String.format("%02d:%02d:%02d", hours, minutes, seconds);
        } else {
            return String.format("%02d:%02d", minutes, seconds);
        }
    }

    public static boolean checkTimepoint(String timepoint) {
        String regex = "[0-9]{1,2}:[0-9]{2}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(timepoint);
        return matcher.matches();
    }

    public static String handlePlaceholders(String str, String replaceStr) {
        int len = str.length();
        int i = 0;
        StringBuilder result = new StringBuilder();
        while (i < len) {
            if (i == len - 1) {
                if (str.charAt(i) == '#') {
                    result.append(replaceStr);
                } else {
                    result.append(str.charAt(i));
                }
                break;
            }
            char c1 = str.charAt(i);
            char c2 = str.charAt(i + 1);
            if (c1 == '#' && c2 == '#') {
                result.append("#");
                i++;
            } else if (c1 == '#') {
                result.append(replaceStr);
            } else {
                result.append(c1);
            }
            i++;
        }
        return result.toString();
    }

    public static boolean isSameTimePoint(String timePoint1, String timePoint2) {
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("H:mm");
            LocalTime t1 = LocalTime.parse(timePoint1, formatter);
            LocalTime t2 = LocalTime.parse(timePoint2, formatter);
            return t1.equals(t2);
        } catch (Exception e) {
            return false;
        }
    }

    public static <T> String listToString(List<T> list) {
        if (list == null) {
            return "[]";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < list.size(); i++) {
            sb.append("\"").append(list.get(i)).append("\"");
            if (i != list.size() - 1) {
                sb.append(", ");
            }
        }
        sb.append("]");
        return sb.toString();
    }

    public static String formatByte(double bytes) {
        String[] suffix = {"B", "KB", "MB", "GB", "TB", "PB", "EB"};
        int i = 0;
        double d = bytes;
        while (d >= 1024) {
            d /= 1024;
            i++;
        }
        return String.format("%.2f%s", d, suffix[i]);
    }
}
